
<template>
  <div id="Qes">
    <Dialog ref="orgPop" :title="title" :open="open" @close="closeOrgDialog">
      <Select
        :lists="lists"
        @confirm="confirm"
        v-model="checked"
        @expand="expand"
        @unexpand="unexpand"
        slot="dialog-content"
      ></Select>
    </Dialog>
  </div>
</template>

<script>
  import Dialog from "@/dialog/Dialog.vue";
  import Select from "@/quseType/QesTreeThrough.vue";

  export default {
    name: "Qes",
    components: {
      Select,
      Dialog
    },
    props: {
      orgType: {
        type: String,
        default: "all"
      },
      title: {
        type: String,
        default: "选择问题类型"
      },
      // 选择结果
      checked: {
        type: Array,
        default: () => {
          return [];
        }
      },
      open: {
        type: Boolean,
        default: false
      },
      customRoot: {
        type: Array,
        default: () => {
          return [];
        }
      }
    },
    model: {
      prop: "checked",
      event: "change"
    },
    data() {
      return {
        api: {
          getOrgTreeDataApi: "/safety/problemType/problemTypeList", // 获取问题类型
        },
        // 组织信息列表
        lists: [],
        pinfo: {
          pid: "",
          pname: "",
          type: ""
        },
        contentList:[],
      };
    },
    computed: {},
    created() {
      this.setPList();
    },
    mounted() {
    },
    watch: {},
    methods: {
      // 打开
      openOrgDialog() {
        this.$emit("open", true);
      },
      // 关闭
      closeOrgDialog() {
        this.$emit("close", true);
      },
      // 确认回调
      confirm(ev) {
        this.$emit("change", ev);
        this.$emit("confirm");
      },
      setPList() {
        this.getTypeList()
      },
      // 展开获取下级
      expand(item) {
        let allData = JSON.parse(sessionStorage.getItem('typeList') || '[]');
        if (allData) {
          this.lists = this.findAllData(item,allData);
          this.getTypeConten(item)
        } else {
          this.getTypeList();
        }
      },
      unexpand(item) {
        let allData = JSON.parse(sessionStorage.getItem('typeList') || '[]');
        if (allData && item.id != '-1') {
          this.lists = this.findAllData(item,allData);
          this.getTypeConten(item)
        } else {
          this.getTypeList();
        }
      },
      // 获取组织信息
      getTypeList(item) {
        this.$A
          .Go("post", '/safety/problemType/problemTypeList', {})
          .then(response => {
            if(response.code == 200){
              let data = response.body || [];
              for(let i = 0; i < data.length; i++){
                if(!data[i]['children']){
                  data[i]['children'] = []
                }
                data[i]['hasChecked'] = false
              }
              data.push({
                problemTypeId:'其他',
                id:'-1',
                hasChecked:true,
              })
              this.lists = data;
              sessionStorage.setItem('typeList',JSON.stringify(data))
            }else{
              this.$A.MS(response.msg)
            }
          })
          .catch(err => {
            console.log(err);
          });
      },
      findAllData(list,data){
        let arr = [];
        for(let i = 0; i < data.length; i++){
          if(data[i]['id'] == list['id']){
            if(data[i]['children'] && data[i]['children'].length){
              arr.push(...data[i]['children']);
            }else{
              // arr.push(...[])
            }
          }else{
            if(data[i]['children'] && data[i]['children'].length){
              arr.push(...this.findAllData(list, data[i]['children']))
            }else{
              //
            }
          }
        }
        return arr;
      },
      // 获取问题内容
      getTypeConten(item) {
        this.$A
          .Go("post", '/safety/problemItem/pageList', {
           "obj":{
             "orgId":this.$A.GS('orgInfo')['orgId'],
             "problemId":item.id
            },
            "pageNo":'-1',
            "pageSize":'0'
          })
          .then(response => {
           if(response.code == 200){
             //contentList
            if(response.body && response.body.rows && response.body.rows.length){
              let data = response.body.rows || [];
              for(let i = 0; i < data.length; i++){
                 data[i]['hasChecked'] = true
              }
              this.contentList = data
              this.lists.push(...data)
            }
            
           }else{
             this.$A.MS(response.msg)
           }
          })
          .catch(err => {
            console.log(err);
          });
      },
    },
    destroyed() {
      sessionStorage.removeItem('typeList')
    }
  };
</script>

<style lang="scss">
</style>
